package com.g13.virtualpethospital.service.Implement;

import com.g13.virtualpethospital.common.EntityCommon;
import com.g13.virtualpethospital.controller.response.LoginResponse;
import com.g13.virtualpethospital.entity.UserEntity;
import com.g13.virtualpethospital.exceptions.UserException;
import com.g13.virtualpethospital.service.UserService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Title: UserServiceImpl
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.service.Implement
 * @Date 24/3/2024 17:21
 * @description: Implement for UserSevice
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<UserEntity> findAllUser() {
        return mongoTemplate.findAll(UserEntity.class);
    }

    @Override
    public List<UserEntity> findUserByType(EntityCommon.RoleType roleType) {
        if (roleType == null) return findAllUser();
        Query query = new Query(Criteria.where("role").is(roleType));
        return mongoTemplate.find(query, UserEntity.class);

    }

    @Override
    public List<UserEntity> findUserByUserName(String username) {
        if (username == null || username == "") return findAllUser();
        Query query = new Query(Criteria.where("username").is(username));
        return mongoTemplate.find(query, UserEntity.class);
    }

    @Override
    public List<UserEntity> findUserByTel(String tel) {
        if (tel == null || tel == "") return findAllUser();
        Query query = new Query(Criteria.where("tel").is(tel));
        return mongoTemplate.find(query, UserEntity.class);
    }

    @Override
    public List<UserEntity> findUserById(String Id) {
        List<UserEntity> userEntityList = new ArrayList<>();
        UserEntity user = mongoTemplate.findById(Id, UserEntity.class);
        if(user != null) userEntityList.add(user);
        return userEntityList;
    }

    @Override
    public void updateUser(UserEntity user, String id) throws UserException {
        UserEntity user1 = mongoTemplate.findById(id, UserEntity.class);
        if(user1 == null) throw new UserException(UserException.UserErrorCode.FIND_NO_USER);
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        if (user.getUsername() != null)
        update.set("username", user.getUsername());
        if (user.getGender() != null)
        update.set("gender", user.getGender());
        if (user.getTel() != null)
        update.set("tel", user.getTel());
        if(user.getPwd()!= null)
        update.set("pwd", user.getPwd());
        if (user.getRole() != null)
        update.set("role", user.getRole());
        UpdateResult result = mongoTemplate.upsert(query, update, UserEntity.class);
    }

    @Override
    public void deleteUser(String id) throws UserException {
        UserEntity user1 = mongoTemplate.findById(id, UserEntity.class);
        if(user1 == null) throw new UserException(UserException.UserErrorCode.FIND_NO_USER);
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult result = mongoTemplate.remove(query,UserEntity.class);
        if(result.getDeletedCount() == 0) throw new UserException(UserException.UserErrorCode.DEL_FAIL);
    }

    @Override
    public LoginResponse login(String username, String pwd) throws UserException {
        List<UserEntity> userEntities = findUserByUserName(username);
        if(userEntities.isEmpty()) throw new UserException(UserException.UserErrorCode.FIND_NO_USER);
        UserEntity user = userEntities.get(0);
        if(!Objects.equals(user.getPwd(), pwd)) throw new UserException(UserException.UserErrorCode.ERROR_PWD);
        return LoginResponse.success();
    }

    @Override
    public LoginResponse register(UserEntity user) {
        user.setId(null);
        mongoTemplate.insert(user);
        return LoginResponse.success();
    }
}
